package com.pat.java.base;
import java.io.UnsupportedEncodingException;
import java.util.LinkedList;
import java.util.List;

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
import com.google.protobuf.InvalidProtocolBufferException;
import com.pat.proto.message.BeautyGirlsBoysProto.BeautyGirlsBoys;
import com.pat.proto.message.BeautyGirlsBoysProto.BeautyGirlsBoys.BeautyGirlBoyMsg;


public class TestMemcached {

	/**
	 * @param args
	 */
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		if(args.length < 5){
			System.out.println("参数错误!0:type(0:get 1:set) 1:poolName 2:servers 3:weights 4:key 5:valueType 6:value");
			return;
		}
		int type = Integer.valueOf(args[0]);
		System.out.println("type="+type);
		
		String poolName = args[1];
		System.out.println("poolName="+poolName);
		
		String[] servers = args[2].split("[,]");
		System.out.println("servers="+servers.toString());
		
		String[] tmpStr = args[3].split("[,]");
		System.out.println("weights="+tmpStr.toString());
		
		Integer[] weights = new Integer[tmpStr.length];// server权重
		for (int i = 0; i < tmpStr.length; i++) {
			weights[i]=Integer.parseInt(tmpStr[i]);
		}
		String key = args[4];
		System.out.println("key="+key);
		
		SockIOPool pool = null;
		int connSize = 3;
		pool = SockIOPool.getInstance(poolName);
		pool.setServers( servers );
		pool.setWeights( weights ); 
		
		pool.setFailover( true );
		// 设置初始连接数、最小和最大连接数
		pool.setInitConn( connSize ); 
		pool.setMinConn( connSize );
		pool.setMaxConn( connSize * 3 );
		// 设置一个连接最大空闲时间6小时
		pool.setMaxIdle( 1000 * 60 * 60 * 6 ); 
		
		// 设置主线程的睡眠时间  (5分钟)
		// pool.setMaintSleep( 300 );  
		
		// 设置TCP的参数 
		// 关闭nagle算法
		pool.setNagle( false );
		// 设置 读取 超时50 毫秒
		pool.setSocketTO( 50 );
		// 不设置连接超时
		pool.setSocketConnectTO(10);
		// 
		pool.setAliveCheck( true );
		
		// 初始化
		pool.initialize();
		
		MemCachedClient memCachedClient = new MemCachedClient(poolName);
		// 设置压缩模式
		//如果超过64k压缩数据
		//memCachedClient.setCompressEnable( true );
		//memCachedClient.setCompressThreshold( 64 * 1024 );

		
		if(type == 0){	//取
			Object obj = memCachedClient.get(key);
			System.out.println("obj is " + obj);
			if(obj instanceof LinkedList){		//仙魔大战消息存储情况
				LinkedList<byte[]> list = (LinkedList<byte[]>) obj;
				if(list == null || list.size() == 0){
					System.out.println("list is null..........");
					return;
				}
				System.out.println("list size is:"+list.size());
				for(byte[] b : list){
					try {
						System.out.println(new String(b, "utf-8"));
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
					}
				}
			}else if(obj instanceof String){
				System.out.println("the string is:"+obj);
			}else if(obj instanceof byte[]){
				System.out.println("obj is byte[]");
				
				byte[] data = (byte[]) obj;
				BeautyGirlsBoys activeMsg = null;
				try {
					activeMsg = BeautyGirlsBoys.parseFrom(data);
				} catch (InvalidProtocolBufferException e) {
					e.printStackTrace();
				}
				List<BeautyGirlBoyMsg> gbs = activeMsg.getBeautyGirlsBoysList();
				for(BeautyGirlBoyMsg msg : gbs){
					System.out.println(msg.getPlayerId()+" : "+msg.getPlayerName()+"===>"+msg.getPlayerVotes());
				}
			}
		}
		/*else if(type == 1){	//存
			Object value = null;
			if(args.length > 5){
				int valueType = Integer.valueOf(args[5]);
				if(valueType == 0){	//Integer
					value = Integer.valueOf(args[6]);
				}else if(valueType == 1){	//String
					value = args[6];
				}else if(valueType == 2){	//List<?>
					value = args[6].split("[,]");
				}
			}
			memCachedClient.set(key, value, new Date(1 * 24 * 60 * 60 * 1000L));
		}*/
	}
}